package top.gbiot.scan.controller;

import com.alibaba.excel.EasyExcel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import top.gbiot.scan.entity.Device;
import top.gbiot.scan.listener.DeviceListener;
import top.gbiot.scan.mapper.CreateDeviceMapper;
import top.gbiot.scan.service.IoTableService;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
//@Controller
@RequestMapping(value = "/iot/scan/controller/device")
public class DeviceController {

    List<Device> deviceList = new ArrayList<>();

    @Autowired
    IoTableService tableService;

    @RequestMapping(value = "import")
    public List<Device> importStudentInfos(MultipartFile file) throws IOException {

        //如果不存在设备信息表则创建
        tableService.createTable();

        System.out.println(" 已经导入了 device  excel 文件， 下一步处理。。。");
        DeviceListener deviceListener = new DeviceListener();
        EasyExcel.read(file.getInputStream(), Device.class, deviceListener).sheet().doRead();

        deviceList = deviceListener.getDeviceList();
        for (Device device : deviceList) {
            //插入导入的数据
            tableService.insertTableDevice(device);
        }

        return deviceList;
    }


}
